package ninja.awesome.game.gameobject;

import java.util.Random;

import ninja.awesome.framework.Graphics;
import ninja.awesome.framework.Image;
import ninja.awesome.framework.Sound;
import ninja.awesome.game.Assets;
import ninja.awesome.game.MainGameScreen;
import ninja.awesome.game.MainGameScreen.PlayingMode;
import ninja.awesome.game.classes.Hostile;
import ninja.awesome.game.compat.Rectangle;
import ninja.awesome.game.main.Animation;
import ninja.awesome.game.main.Controller;
import ninja.awesome.game.main.Physics;
import ninja.awesome.game.main.Sprites;
import android.graphics.Color;

public class Enemy extends GameObj implements Hostile {

	Random r = new Random();
	private int robotID;
	private int width = 150;
	private int height = 167;
	private int enemyHealth = 50;
	private int maxHealth = 50;
	private int speed;
	private int tempSpeed;

	private int deadAnim;
	private int standAnim;
	private int walkAnim;
	private int attackAnim;
	private int numAnim;

	private int walkLeftAction;
	private int walkRightAction;
	private int attackLeftAction;
	private int attackRightAction;
	private int hitLeftAction;
	private int hitRightAction;
	private int deadLeftAction;
	private int deadRightAction;
	private double xpAmountDropped = 10;
	private Image[] image;
	private Image[] e;
	private int ticks;
	private int timer = 0;
	private int robotRecoverTimer = 0;
	private boolean isDead = false;
	private boolean isHit = false;
	private boolean isAttack = false;
	private String direction = "left";
	private int randNumber = 0;

	Sound boom;
	MainGameScreen game;
	Controller c;
	Sprites s;
	Animation anim, explosion;

	public Enemy(int x, int y, int speed, Controller c, MainGameScreen game, Sprites s) {
		super(x, y);
		this.s = s;
		this.c = c;

		// robot id is humanoid robot
		enemyHealth = 50;
		this.speed = (r.nextInt(3) + 2) * speed;
		tempSpeed = this.speed;
		this.game = game;
		image = s.getEnemySprite();
		e = s.getExplosion2();

		deadAnim = 6; // number of humanoid robot animation display
		standAnim = 3;
		walkAnim = 11;
		attackAnim = 8;
		numAnim = walkAnim;

		walkLeftAction = 0; // number that indicate location of piece of
							// picture
		walkRightAction = 1; // for animation
		attackLeftAction = 2;
		attackRightAction = 3;
		hitLeftAction = 4;
		deadLeftAction = 5;

		explosion = new Animation(10, image[0], 185, 150, 5);
		anim = new Animation(1, image[0], 185, width, numAnim);
		boom = Assets.boom;

	}

	@Override
	public void update() {
		ticks++;
		if (isHit)
			robotRecoverTimer++;

		if (isDead) {

			explosion.runtAnimationWithoutLoop();
			if (direction.equals("left")) {
				explosion.setAction(6);
			} else
				explosion.setAction(7);
			if (timer == 40) {
				c.getEnemyList().remove(this);
				setIsDead(false);
				setTimer(0);
				// 1P
				game.getNinja().setScore(game.getNinja().getScore());
				game.getNinja().setExperiencePoint(game.getNinja().getExperiencePoint() + xpAmountDropped);

				game.setEnemy_killed(game.getEnemy_killed() + 1);
				game.setTotalKilled(game.getTotalKilled() + 1);
			}
			timer++;
			// PHU: update position related to speed of ninja
			xLoc -= game.getNinja().getSpeedX();
		} else { // is where I put no enemy plane spawn!!! I set
					// !isDead
			// --> isDead to test the player
			// MOVEMENT

			if (!isAttack && !isHit) {
				this.speed = this.tempSpeed;
				anim.setSpeed(3);
				if (game.getNinja().getX() < this.getX() - 15) {
					anim.setAction(0); // fix them up!

					anim.setImageNumber(walkAnim);
					direction = "left";

					xLoc -= (speed);
				}
				if (game.getNinja().getX() > this.getX() + 15) {
					anim.setAction(1); // fix them up

					anim.setImageNumber(walkAnim);
					direction = "right";

					xLoc += (speed);
				}
				// PHU: update position related to speed of ninja
				xLoc -= game.getNinja().getSpeedX();

				if (game.getNinja().getY() < this.getY()) {

					numAnim = walkAnim;
					yLoc -= speed;
					if (yLoc < 350)
						yLoc = 349;
				}
				if (game.getNinja().getY() > this.getY()) {

					numAnim = walkAnim;
					yLoc += speed;
					if (yLoc > 450)
						yLoc = 449;
				}

			}
			// BOUNDS
			if (yLoc >= (MainGameScreen.HEIGHT + 10)) {
				yLoc = -15;
				xLoc = r.nextInt(700);
			}

			// Slashing AI
			// delay to shoot until timer 60.
			for (int i = 0; i < game.fl.size(); i++) {
				Attack tempFriend = (Attack) game.fl.get(i);

				// COLLISION WITH BULLETS
				if (Physics.collision(this, tempFriend)) {
					randNumber = r.nextInt(101);
					tempFriend.setHit(true);
					this.isHit = true;

					// I put 5 as constant damage from ninja's weapon
					// this has to be expand for different weapons
					this.setHealth(enemyHealth - 5);

					if (enemyHealth < 0) {
						this.setIsDead(true);
						boom.play(1f);
						// 1P
						if (game.getPlayingMode() == PlayingMode.SINGLE) {
							game.getNinja().setScore(game.getNinja().getScore() + 15);
						}
						// 2P
						if (game.getPlayingMode() == PlayingMode.MULTIPLE) {
							game.getNinja().setScore(game.getNinja().getScore() + 15);
							game.getNinja2().setScore(game.getNinja2().getScore() + 15);
						}
					}
				}
			}
			if (!isHit) {

				if (Physics.collision(game.getNinja(), this) && ticks % 60 == 0 && anim.getTempNumber() <= attackAnim) {
					anim.setTempNumber(0);
					if (direction.equals("left")) // attack left
						anim.setAction(2);
					if (direction.equals("right")) // attack right
						anim.setAction(3);

					anim.setImageNumber(attackAnim); // number of attack
														// animation
					anim.setSpeed(6);
					this.SetY(yLoc - 10); // display stable position for
											// attack
											// animation
					isAttack = true;
					c.addHostile(new EnemyAttack(this.xLoc, this.yLoc, this.speed + 1, this.game, this.c, this.s,
							this.direction, 0));

				}

				if (anim.getTempNumber() >= attackAnim - 1) // enemy swing
															// animation display
					isAttack = false; // completed attack
				//

			}

			if (isHit) { // this is where to fix!
				if (randNumber < 20) {
					// System.out.println("randomNumber= " + d);
					if (direction.equals("left")) {
						anim.setAction(5);

					} else
						anim.setAction(6);
					this.speed = 0;
					anim.setAction(4);
					// anim.setImageWidth(150);
					anim.setImageNumber(2);
					this.isAttack = false;
					if (robotRecoverTimer >= 40) {
						isHit = false;
						this.robotRecoverTimer = 0;

					}
				} else
					this.isHit = false;

			}
		}
		anim.runAnimation();
	}

	@Override
	public void draw(Graphics g) {
		if (isDead) {
			explosion.drawAnimation(g, xLoc, yLoc + 10);
		} else {
			anim.drawAnimation(g, xLoc, yLoc);
			/*
			 * g.fillRect(xLoc + 100, yLoc - 20, 50, 20, Color.RED);
			 *
			 * if (enemyHealth >= 0) { g.fillRect(xLoc + 100, yLoc - 20,
			 * this.getHealth(), 20, Color.GREEN); } g.drawRect(xLoc + 100, yLoc
			 * - 20, 50, 20, Color.GRAY);
			 */
			// PHU: draw percent
			g.fillRect(xLoc + 100, yLoc - 20, 50, 20, Color.RED);
			if (enemyHealth >= 0) {
				g.fillRect(xLoc + 100, yLoc - 20, (int) ((float) this.getHealth() / this.getMaxHealth() * 50f), 20,
						Color.GREEN);
			}
			g.drawRect(xLoc + 100, yLoc - 20, 50, 20, Color.GRAY);
		}

	}

	@Override
	public Rectangle getBounds() {
		return new Rectangle(xLoc, yLoc, 100, 100);
	}

	// GETTERS
	@Override
	public int getX() {
		return xLoc;
	}

	@Override
	public int getY() {
		return yLoc;
	}

	public void setTimer(int timer) {
		this.timer = timer;
	}

	public double getxpAmountDropped() {
		return xpAmountDropped;
	}

	@Override
	public boolean getIsDead() {
		return isDead;
	}

	@Override
	public void setIsDead(boolean isDead) {
		this.isDead = isDead;
	}

	public void SetX(int x) {
		this.xLoc = x;
	}

	public void SetY(int y) {
		this.yLoc = y;
	}

	public void setxpAmountDropped() {
		this.xpAmountDropped = xpAmountDropped;
	}

	public int getTimer() {
		return timer;
	}

	public int getHealth() {
		return enemyHealth;
	}

	public void setHealth(int Health) {
		this.enemyHealth = Health;
	}

	public int getMaxHealth() {
		return maxHealth;
	}

	public void setMaxHealth(int maxHealth) {
		this.maxHealth = maxHealth;
	}

}
